package com.zsc.oj.utils;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.CIObject;
import com.qcloud.cos.model.ciModel.persistence.ImageInfo;
import com.qcloud.cos.model.ciModel.persistence.ProcessResults;
import com.zsc.oj.common.ErrorCode;
import com.zsc.oj.constant.FileConstant;
import com.zsc.oj.exception.BusinessException;
import com.zsc.oj.manager.CosManager;
import com.zsc.oj.model.entity.User;
import com.zsc.oj.model.enums.FileUploadBizEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Slf4j
public class UploadFileUtils<T,S extends IService<T>>{

    @Autowired
    S service;

    @Resource
    private CosManager cosManager;

    public boolean upload(FileUploadBizEnum fileUploadBizEnum, MultipartFile multipartFile, User loginUser){
        // 文件目录：根据业务、用户来划分
        String uuid = RandomStringUtils.randomAlphanumeric(8);
        String filename = uuid + "-" + multipartFile.getOriginalFilename();
        String filepath = String.format("/%s/%s/%s", fileUploadBizEnum.getValue(), loginUser.getId(), filename);
        File file = null;
        try {
            // 上传文件
            file = File.createTempFile(filepath, null);
            multipartFile.transferTo(file);
            PutObjectResult putObjectResult = cosManager.putObject(filepath, file);
//            ImageInfo imageInfo = putObjectResult.getCiUploadResult().getOriginalInfo().getImageInfo();
//            ProcessResults processResults = putObjectResult.getCiUploadResult().getProcessResults();
//
//            List<CIObject> objectList = processResults.getObjectList();
//            if(CollUtil.isNotEmpty(objectList)){
//
//                CIObject compressedCiObject = objectList.get(0);
//
//            }
            // 返回可访问地址
            String userAvatarPath = FileConstant.COS_HOST + filepath;
            UpdateWrapper<T> userUpdateWrapper = new UpdateWrapper<>();
            userUpdateWrapper.set("userAvatar",userAvatarPath);
            userUpdateWrapper.eq("id",loginUser.getId());
            boolean result = service.update(userUpdateWrapper);

            return result;
        } catch (Exception e) {
            log.error("file upload error, filepath = " + filepath, e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "上传失败");
        } finally {
            if (file != null) {
                // 删除临时文件
                boolean delete = file.delete();
                if (!delete) {
                    log.error("file delete error, filepath = {}", filepath);
                }
            }
        }

    }
}
